package org.dromara.system.controller.chat;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysChatContactBo;
import org.dromara.system.domain.vo.SysChatContactVo;
import org.dromara.system.service.ISysChatContactService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会话列表
 *
 * @author Lion Li
 * @date 2025-02-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/chat/contact")
public class SysChatContactController extends BaseController {

    private final ISysChatContactService sysChatContactService;

    /**
     * 查询会话列表列表
     */
    @SaCheckPermission("system:contact:list")
    @GetMapping("/list")
    public TableDataInfo<SysChatContactVo> list(SysChatContactBo bo, PageQuery pageQuery) {
        return sysChatContactService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会话列表列表
     */
    @SaCheckPermission("system:contact:export")
    @Log(title = "会话列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysChatContactBo bo, HttpServletResponse response) {
        List<SysChatContactVo> list = sysChatContactService.queryList(bo);
        ExcelUtil.exportExcel(list, "会话列表", SysChatContactVo.class, response);
    }

    /**
     * 获取会话列表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:contact:query")
    @GetMapping("/{id}")
    public R<SysChatContactVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(sysChatContactService.queryById(id));
    }

    /**
     * 新增会话列表
     */
    @SaCheckPermission("system:contact:add")
    @Log(title = "会话列表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysChatContactBo bo) {
        return toAjax(sysChatContactService.insertByBo(bo));
    }

    /**
     * 修改会话列表
     */
    @SaCheckPermission("system:contact:edit")
    @Log(title = "会话列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysChatContactBo bo) {
        return toAjax(sysChatContactService.updateByBo(bo));
    }

    /**
     * 删除会话列表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:contact:remove")
    @Log(title = "会话列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(sysChatContactService.deleteWithValidByIds(List.of(ids), true));
    }
}
